console.log("loading: window.js");
function WindowControl(params) {
	// console.log("WindowControl::ctor()");
	// windowTemplate
	// isModal
	if (params == undefined) {
		params = {};
	}
	if (params.containerSelector != undefined) {
		this.$container = $(params.containerSelector);
	} else {
		this.$container = $(document.body);
	}
	this.windowTemplate = (params.windowTemplate == undefined ? "window/modal"
			: params.windowTemplate);
	this.tmpValue = {};
	this.tmpBind = {};
	this.tmpValueAfter = {};
	this.events = {};

	if (params.values != undefined) {
		var tmp = params.values;
		var tmpV = this.tmpValue;
		for ( var i in tmp) {
			tmpV[i] = tmp[i];
		}
	}

	if (params.binds != undefined) {
		var tmp = params.binds;
		var tmpV = this.tmpBind;
		for ( var i in tmp) {
			tmpV[i] = tmp[i];
		}
	}

	this.isModal = params.isModal;
}

/*
 * function test1() { console.log(window.innerWidth,"x",window.innerHeight,"
 * ",window.offsetHeight); }
 */

WindowControl.prototype = {
	ControlSelector : {
		Header : '.modal-window-header',
		Footer : '.modal-window-footer',
		Content : '.modal-window-content',
		HeaderTitle : '.modal-window-header-title',
		HeaderIcon : '.modal-window-header-icon',
		HeaderClose : '.modal-window-header-close'
	},
	Init : function(onInitBefore, onInitAfter) {
		/* console.log("WindowControl::Init()"); */
		var mainControl = this;
		function init() {
			/* console.log("WindowControl::init()"); */
			if (mainControl.isClosing == true) {
				return;
			}
			mainControl.$window = $(TM_FillTemplateData(mainControl.windowTemplate));
			mainControl.ParseDOM();

			if (mainControl.isHidden == false) {
				mainControl.$window.hide();
			}
			if (mainControl.isModal == true) {
				mainControl.modalShadow = new BackgroundShadow(
						mainControl.$container).Show();
			}
			if (onInitBefore) {
				onInitBefore(mainControl);
			}
			mainControl.$window.appendTo(mainControl.$container);

			mainControl.$window = mainControl.$window.not('.template-name');

			LockUnlockScroll(true);

			if (mainControl.tmpValueAfter != undefined) {
				var tmp = mainControl.tmpValueAfter;
				for ( var i in tmp) {
					mainControl[i](tmp[i]);
				}
				delete (mainControl.tmpValueAfter);
			}

			if (onInitAfter) {
				onInitAfter(mainControl);
			}
		}
		TM_GetTemplate(this.windowTemplate, init);
		return this;
	},
	onClose : function(e) {
		/* console.log("WindowControl::onClose()"); */
		if (e.data) {
			e.data.Close();
		}
	},
	Center : function(params) {
		/* console.log("WindowControl::Center()"); */
		if (!this.isInit) {
			this.tmpValueAfter.Center = params;
			return this;
		}
		if (params == undefined) {
			params = {};
		}
		var tmp = params.target == undefined ? this.$container
				: $(params.target);
		var offset = params.offset == undefined ? {
			left : 0,
			top : 0
		} : params.offset;

		var yPos = 0;
		if (tmp[0] == document.body) {
			yPos = document.documentElement.scrollTop;
			if (yPos == 0) {
				if (window.pageYOffset)
					yPos = window.pageYOffset;
				else
					yPos = (document.body.parentElement) ? document.body.parentElement.scrollTop
							: 0;
			}
			/*
			 * Old yPos =
			 * yPos+(document.body.clientHeight-this.Height())/2+offset.top+"px";
			 */
		} else {
			yPos = (tmp.height() - this.Height()) / 2 + offset.top;
		}
		this.Left((tmp.width() - this.Width()) / 2 + offset.left);

		this.Top(yPos - this.Height());

		return this;
	},
	ParseDOM : function() {
		/* console.log("WindowControl::ParseDOM()"); */
		if (this.isInit == true) {
			return this;
		}
		if (this.$window == undefined) {
			this.$window = this.$container;
		}
		var tmpC = this.$window;
		var s = this.ControlSelector;

		this.$content = $(s.Content, tmpC);
		this.$header = $(s.Header, tmpC);
		this.$footer = $(s.Footer, tmpC);
		this.$header_title = $(s.HeaderTitle, tmpC);
		this.$header_icon = $(s.HeaderIcon, tmpC);
		this.$header_close = $(s.HeaderClose, tmpC).bind('click', this,
				this.onClose);
		this.isInit = true;

		if (this.tmpValue != undefined) {
			var tmp = this.tmpValue;
			for ( var i in tmp) {
				this[i](tmp[i]);
			}
			delete (this.tmpValue);
		}
		if (this.tmpBind != undefined) {
			var tmp = this.tmpBind;
			for ( var i in tmp) {
				this.Bind(i, tmp[i]);
			}
			delete (this.tmpBind);
		}

		return this;
	},
	showBackground : function() {
		/* console.log("WindowControl::showBackground()"); */
		if (!this.isInit) {
			this.$container.oneTime(100, jQuery
					.proxy(this.showBackground, this));
		} else if (this.isHidden != true) {
			this.Show();
		}
	},
	Show : function() {
		/* console.log("WindowControl::Show()"); */
		this.isHidden = false;
		if (!this.isInit) {
			this.$container.oneTime(100, jQuery
					.proxy(this.showBackground, this));
			return this;
		}
		if (this.$window != undefined) {
			this.$window.show().animate( {
				top : parseInt(this.Top(), 10) + parseInt(this.Height(), 10)
			}, 300).effect("bounce", {
				times : 2,
				distance : 3
			}, 200, this.events['onShow']);
		}
		return this;
	},
	Hide : function() {
		/* console.log("WindowControl::Hide()"); */
		this.isHidden = true;
		if (this.$window != undefined) {
			this.$window.hide();
		}
		return this;
	},
	Draggable : function() {
		/*
		 * console.log("WindowControl::Draggable()"); if (!this.isInit) {
		 * this.tmpValue.Draggable = true; } else { this.$window.draggable({
		 * containment: 'parent', handle:this.$header }); }
		 */
		return this;
	},
	Header : function(value) {
		/* console.log("WindowControl::Header()"); */
		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Header;
			} else {
				this.tmpValue.Header = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$header;
		} else {
			this.$header.empty().append(value);
			return this;
		}
	},
	Footer : function(value) {
		/* console.log("WindowControl::Footer()"); */
		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Footer;
			} else {
				this.tmpValue.Footer = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$footer;
		} else {
			this.$footer.empty().append(value);
			return this;
		}
	},
	Title : function(value) {
		/* console.log("WindowControl::Title()"); */
		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Title;
			} else {
				this.tmpValue.Title = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$header_title.text();
		} else {
			this.$header_title.text(value);
			return this;
		}
	},
	Icon : function(value) {
		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Icon;
			} else {
				this.tmpValue.Icon = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$header_icon;
		} else {
			this.$header_icon.empty().append(value);
			return this;
		}
	},
	Width : function(value) {

		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Width;
			} else {
				this.tmpValue.Width = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$window.width();
		} else {
			this.$window.width(value);
			return this;
		}
	},
	Height : function(value) {

		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Height;
			} else {
				this.tmpValue.Height = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$window.height();
		} else {
			this.$window.height(value);
			return this;
		}
	},
	Content : function(value) {

		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Content;
			} else {
				this.tmpValue.Content = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$content;
		} else {
			this.$content.empty().append(value);
			return this;
		}
	},
	Left : function(value) {

		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Left;
			} else {
				this.tmpValue.Left = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$window.css('left');
		} else {
			this.$window.css('left', value);
			return this;
		}
	},
	Top : function(value) {

		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpValue.Top;
			} else {
				this.tmpValue.Top = value;
				return this;
			}
		}

		if (value == undefined) {
			return this.$window.css('top');
		} else {
			this.$window.css('top', value);
			return this;
		}
	},
	Bind : function(type, value) {

		if (type == undefined) {
			return this;
		}

		if (!this.isInit) {
			if (value == undefined) {
				return this.tmpBind[type];
			} else {
				this.tmpBind[type] = value;
				return this;
			}
		}

		if (value == undefined) {
			var tmp = this.events[type];
			if (tmp != undefined) {
				tmp(this);
			}
		} else {
			this.events[type] = value;
		}
		return this;
	},
	Unbind : function(type/* ,value */) {

		if (type == undefined) {
			return this;
		}
		if (!this.isInit) {
			delete (this.tmpBind[type]);
		} else {
			delete (this.events[type]);
		}
		return this;
	},
	Close : function() {

		if (this.isClosing == true) {
			return this;
		}
		this.isClosing = true;
		if (this.events.close) {
			this.events.close(this);
		}
		if (this.isInit) {
			this.$window.animate( {
				top : parseInt(this.Top(), 10) - parseInt(this.Height(), 10)
			}, 500, function() {
				$(this).remove();
			});

			if (this.modalShadow) {
				this.modalShadow.Hide().Remove();
			}
		}
		LockUnlockScroll(false);
		return this;
	}
};

function DialogWindowControl(params) {
	/* console.log("DialogWindowControl::ctor()"); */
	/* buttons */
	if (params == undefined) {
		params = {};
	}
	params.isModal = true;
	params.windowTemplate = "window/dialog";
	var win = new WindowControl(params);

	function init_override(onInitBefore, onInitAfter) {
		// console.log("DialogWindowControl::init_override()");
		var $buttons;
		var tmpB = {};
		var tmp = params.buttons;
		function init() {
			// console.log("DialogWindowControl::init()");
			function initBefore() {
				// console.log("DialogWindowControl::initBefore()");
				$buttons = $('.buttons-pane', win.$window);
				for ( var i in tmp) {
					tmpB[i] = $(
							TM_FillTemplateData("window/dialog.button", tmp[i]))
							.appendTo($buttons);
				}
				if (onInitBefore) {
					onInitBefore(win);
				}
			}

			function initAfter() {
				// console.log("DialogWindowControl::initAfter()");
				var closeEvent = jQuery.proxy(win.Close, win);
				for ( var i in tmp) {
					if (tmp[i].click != undefined) {
						tmpB[i].click(tmp[i].click);
					}
					if (tmp[i].autoClose != false) {
						tmpB[i].click(closeEvent);
					}
				}
				if (onInitAfter) {
					onInitAfter(win);
				}
				if (params.autoTimeClose != undefined) {
					win.$container.oneTime(params.autoTimeClose, closeEvent);
				}
			}
			WindowControl.prototype.Init.call(win, initBefore, initAfter);
		}
		TM_GetTemplate("window/dialog.button", init);
		return win;
	}
	win.Init = init_override;
	return win;
}
TM_GetTemplates( {
	"window/dialog" : {},
	"window/dialog.button" : {}
});